package com.wdcg.activity;

import java.util.ArrayList;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.wdcg.ApplicationSharedPreferences;
import com.wdcg.R;
import com.wdcg.activity.listener.OnNavigateToFragmentListener;
import com.wdcg.fragment.AchievementsFragment;
import com.wdcg.fragment.BonusInfoFragment;
import com.wdcg.fragment.BonusesFragment;
import com.wdcg.fragment.HelpFragment;
import com.wdcg.fragment.MapFragment;
import com.wdcg.fragment.RadarFragment;
import com.wdcg.model.access.BonusAccess;
import com.wdcg.model.access.DwarfAccess;
import com.wdcg.model.dao.BonusDescription;
import com.wdcg.model.dao.Dwarf;
import com.wdcg.model.dao.DwarfDescription;
import com.wdcg.utils.GameLogic;

public class MainActivity extends FragmentActivity implements TabListener, OnNavigateToFragmentListener {

	private static final int STARTING_DWARFS_COUNT = 5;
	private ActionBar actionBar;
	private int activeTab;
	
	private Fragment previousFragment;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionBar.setDisplayOptions(actionBar.getDisplayOptions() ^ ActionBar.DISPLAY_SHOW_TITLE ^ ActionBar.DISPLAY_SHOW_HOME);
        createNewTab("Mapa");
        createNewTab("Radar");
        
        setupGame();
        
    }
   
	public void onTabReselected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub
		
	}

	public void onTabSelected(Tab tab, FragmentTransaction ft) {
		
		android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
		activeTab = tab.getPosition();
		
		switch(tab.getPosition()){
			case 0: 
				MapFragment mapFragment = new MapFragment();
				fragmentTransaction.replace(R.id.fragmentContainer, mapFragment);
				fragmentTransaction.commit();
				
				break;
				
			case 1:
				RadarFragment radarFragment = new RadarFragment();
				fragmentTransaction.replace(R.id.fragmentContainer, radarFragment);
				fragmentTransaction.commit();
		}
		
	}

	public void onTabUnselected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub
		
	}
	
	private void createNewTab(String name){
		
		Tab tab = actionBar.newTab();
        tab.setText(name);
        tab.setTabListener(this);
        actionBar.addTab(tab);        
		
	}
		
	@Override
	  public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.bottom_menu, menu);
	    return true;
	  } 
		
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
		
		switch(item.getItemId()) {
			case R.id.action_help:		
				setPreviousFragment(activeTab == 0 ? new MapFragment() : new RadarFragment());
				fragmentTransaction.replace(R.id.fragmentContainer, new HelpFragment());
				fragmentTransaction.commit();
				
				actionBar.setDisplayHomeAsUpEnabled(true);
		        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
		        actionBar.setDisplayOptions(actionBar.getDisplayOptions() | ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_HOME);
		        actionBar.setTitle(R.string.action_help_title);
				break;
			case R.id.action_bonuses:
				setPreviousFragment(activeTab == 0 ? new MapFragment() : new RadarFragment());
				fragmentTransaction.replace(R.id.fragmentContainer, new BonusesFragment());
				fragmentTransaction.commit();
				
				actionBar.setDisplayHomeAsUpEnabled(true);
		        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
		        actionBar.setDisplayOptions(actionBar.getDisplayOptions() | ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_HOME);
		        actionBar.setTitle(R.string.action_bonuses_title);
				break;
			case android.R.id.home:
				fragmentTransaction.replace(R.id.fragmentContainer, previousFragment);
				fragmentTransaction.commit();
				
				if(previousFragment instanceof MapFragment || previousFragment instanceof RadarFragment) {
					actionBar.setDisplayHomeAsUpEnabled(false);
			        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
			        actionBar.setDisplayOptions(actionBar.getDisplayOptions() ^ ActionBar.DISPLAY_SHOW_TITLE ^ ActionBar.DISPLAY_SHOW_HOME);
			        setPreviousFragment(null);
				}
				else {
					actionBar.setDisplayHomeAsUpEnabled(true);
			        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
			        actionBar.setDisplayOptions(actionBar.getDisplayOptions() | ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_HOME);
			        setPreviousFragment(activeTab == 0 ? new MapFragment() : new RadarFragment());
				}
				
				break;
			case R.id.action_achievements:
				setPreviousFragment(activeTab == 0 ? new MapFragment() : new RadarFragment());
				fragmentTransaction.replace(R.id.fragmentContainer, new AchievementsFragment());
				fragmentTransaction.commit();
				
				actionBar.setDisplayHomeAsUpEnabled(true);
		        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
		        actionBar.setDisplayOptions(actionBar.getDisplayOptions() | ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_HOME);
		        actionBar.setTitle(R.string.action_achievements_title);
				break;
			case R.id.action_new_game:
				for (DwarfDescription lDwarfDescription : DwarfAccess.getDwarvesDescriptions()) {
					Dwarf lDwarf = lDwarfDescription.getDwarf();
					DwarfAccess.setDwarfGameScore(lDwarf, false, false, false);
				}
				
				for (BonusDescription lBonusDescription : BonusAccess.getBonusesDescriptions()) {
					BonusAccess.clearBonusTypeForBonusDescription(lBonusDescription);
				}
				
				DwarfAccess.setNRandomDwarfAsVisible(STARTING_DWARFS_COUNT);
				
				MapFragment mapFragment = new MapFragment();
				fragmentTransaction.replace(R.id.fragmentContainer, mapFragment);
				fragmentTransaction.commit();
				break;
			default:
				ArrayList<DwarfDescription> dwarves=DwarfAccess.getDwarvesDescriptions();
				//How to start dwarf puzzle game
//				Intent intent = new Intent(MainActivity.this,PuzzleGameActivity.class);
//				intent.putExtra("DwarfID", dwarves.get(0).getDwarf().getId());
//	            startActivity(intent);
				//How to start dwarf quiz game
				Intent intent = new Intent(MainActivity.this,ARGameActivity.class);
				startActivity(intent);
				break;
		}
		return true;
	}

	public void setPreviousFragment(Fragment previousFragment) {
		this.previousFragment = previousFragment;
	}
	
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK) {
			if(previousFragment != null) {
				android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
				fragmentTransaction.replace(R.id.fragmentContainer, previousFragment);
				fragmentTransaction.commit();
				if(previousFragment instanceof MapFragment || previousFragment instanceof RadarFragment) {
					actionBar.setDisplayHomeAsUpEnabled(false);
			        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
			        actionBar.setDisplayOptions(actionBar.getDisplayOptions() ^ ActionBar.DISPLAY_SHOW_TITLE ^ ActionBar.DISPLAY_SHOW_HOME);
			        setPreviousFragment(null);
				}
				else {
					actionBar.setDisplayHomeAsUpEnabled(true);
			        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
			        actionBar.setDisplayOptions(actionBar.getDisplayOptions() | ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_HOME);
			        setPreviousFragment(activeTab == 0 ? new MapFragment() : new RadarFragment());
				}
					
			}
			else
				finish();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	public void showBonusInfo(int bonusDescriptionId) {
		BonusInfoFragment bonusInfoFragment = new BonusInfoFragment();
		Bundle bundle = new Bundle();
		bundle.putInt(getString(R.string.bundle_bonus_descr_id), bonusDescriptionId);
		bonusInfoFragment.setArguments(bundle);

		setPreviousFragment(new BonusesFragment());

		android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
		fragmentTransaction.replace(R.id.fragmentContainer, bonusInfoFragment);
		fragmentTransaction.commit();
		actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setDisplayOptions(actionBar.getDisplayOptions() | ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_HOME);        

	}
	
	//ustawia preferencje przy pierwszym uruchomieniu aplikacji
	private void setupGame(){
		
		if(ApplicationSharedPreferences.isNewGame()){
			ApplicationSharedPreferences.setNumberOfDwarfs(5);
			ApplicationSharedPreferences.setNewGamePlayed();
			
			GameLogic.newGame(this);
		}
	}

	public void OnNavigate(Fragment aFragment, int aTitleStringId) {
		
		android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
		
		setPreviousFragment(activeTab == 0 ? new MapFragment() : new RadarFragment());
		fragmentTransaction.replace(R.id.fragmentContainer, aFragment);
		fragmentTransaction.commit();
		
		actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setDisplayOptions(actionBar.getDisplayOptions() | ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_HOME);
        actionBar.setTitle(aTitleStringId);
	}
	
}

